package com.itheima.service;

import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import com.itheima.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class UserService {

    @Test
    public void findAll(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> list = mapper.findAll();
        for (User user : list) {
            System.out.println(user);
        }
        sqlSession.close();
    }
    @Test
    public void findByCondition(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        user.setUsername("辽神");
        user.setPassword("111");
        List<User> list = mapper.findByCondition(user);
        for (User usr : list) {
            System.out.println(usr);
        }
        sqlSession.close();
    }

    @Test
    public void findUsersByCondition(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<Integer> listI = new ArrayList<>();
        listI.add(1);
        listI.add(3);
        listI.add(7);
        List<User> list = mapper.findUsersByCondition(listI);
        for (User usr : list) {
            System.out.println(usr);
        }
        sqlSession.close();
    }

    @Test
    public void updateUser(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        user.setUsername("关羽");
        user.setPassword("000");
        user.setId(1);
        mapper.updateUser(user);

        sqlSession.close();
    }




}
